package com.lin.musicplayer.feature;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.lin.musicplayer.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

/**
 * Created by crystal on 2016/7/8.
 */
public class Online extends Activity {
    EditText urlpath,downpath;
    ImageButton downloadbtn,playbtn;
    String name,path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.online);
        findView();
        downloadbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                path=urlpath.getText().toString();
                name=downpath.getText().toString();
                byte[] bname=name.getBytes();
                try {
                    name=new String(bname,"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                new Thread()
                {
                    @Override
                    public void run() {
                        try{
                            URL url=new URL(path);
                            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                            conn.setConnectTimeout(6*1000);
                            System.out.println("i am here 1");
                            if(conn.getResponseCode()!=200)
                            {
                                throw new RuntimeException("请求url失败");
                            }
                            InputStream is=conn.getInputStream();
                            System.out.println("i am here2");
                            readAsFile(is,new File("/sdcard/mp3/"+name+".mp3"));

                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                Toast.makeText(Online.this,"下载成功",Toast.LENGTH_SHORT).show();
            }
        });
        playbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Online.this,"allldlafdlfadsf",Toast.LENGTH_SHORT).show();
            }
        });
        //Toast.makeText(Online.this,"下载成功",Toast.LENGTH_SHORT).show();
    }
    public void findView()
    {
        urlpath= (EditText)findViewById(R.id.musicUrl);
        downpath=(EditText)findViewById(R.id.putto);
        downloadbtn=(ImageButton)findViewById(R.id.download);
        playbtn=(ImageButton)findViewById(R.id.urlpay);
    }
    public void readAsFile(InputStream inputStream, File file)
    {
        try {
            FileOutputStream outputStream=new FileOutputStream(file);
            byte[] buffer=new byte[1024];
            int len=-1;
            while ((len=inputStream.read(buffer))!=-1)
            {
                outputStream.write(buffer,0,len);
                System.out.println("on\n");
            }
            System.out.println("hdjshdjahdjfhasjdfhasjkdhlksdjasldjf\n");
            outputStream.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
